package com.css.apps.base.user.model;

import com.css.apps.base.common.Constants;
import com.css.apps.base.func.model.SFunc;
import com.css.apps.base.func.model.SFuncAction;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.post.model.SPost;
import com.css.apps.base.role.model.SRole;
import com.css.apps.base.sys.model.SSys;
import com.css.core.model.IUser;
import com.css.db.query.JoinList;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Json.Unjson;
import com.css.util.StringHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@SuppressWarnings("serial")
public class SUser implements Serializable, IUser {


	private String uuid;
	private String realName;
	private String loginName;
	private String reviewPwd;
	private String password;
	private String sex;
	private String mobile;
	private String phone;
	private String email;
	private String userType;
	private String orderNum;
	private String delFlag;
	private String openFlag;
	private Date issueDate;
	private String issueId;
	private String issueName;
	private Date editDate;
	private String remark;
	private Date lastLoginTime;
	private Integer totalLoginCount;
	private Integer failedLoginCount;
	private String orgId;// reference
	private String orgName;
	private Date editPwdTime;
	private String activeStatus;
	private Date activeDeadLine;
	/** 密级 */
	private String secLevel;
	private String skinId;
	/** 工号 */
	private String staffId;
	/** 职务 */
	private String job;
	/** 房间号 */
	private String roomNum;
	private Date lastmodifytime;

	//新增业务字段 Mr.X
	private Date birthday;  			//生日
	private String idcard;	  			//身份证号
	private String contactor;  			//联系人
	private String contactphone;  		//联系方式
	private String nativeplace;  		//籍贯
	private String birthplace;  		//出生地
	private String liveplace;  			//成长地
	private String nation;  			//民族
	private String health;  			//健康状况
	private Date joinworktime;  		//加入工作时间 (年月)
	private Date joinpartytime;  		//入党时间 （年月）
	private String political_landscape; //政治面貌
	private String joinpartycode;		//入党时间文字
	private String secondparty;  		//第二党派
	private String thirdparty;	 		//第三党派
	private String manage_state; 		//人员管理状态
	private String person_type;  		//人员类别
	private String manager_type; 		//管理类别
	private String skill;        		// 熟悉专业有何特长
	private String professiontech_duty; //专业技术职务
	private String resume;				// 简历
	private String current_duty;		//现任职务
	private String reward_and_punish;	//奖惩综述
	private String examine_conclusion;  //年度考核结果
	private String full_time_education; //学历、学位(全日制)
	private String full_time_school_major;//毕业院校即专业（全日制）
	private String on_the_job_education; //学历、学位(在职)
	private String on_the_job_school_major;//毕业院校即专业（在职）


	// 功能列表
	private java.util.Set<String> functions = new java.util.HashSet<String>();
	// 功能点列表
	private java.util.Set<String> funcActions = new java.util.HashSet<String>();
	// 所拥有系统
	private List<SSys> syss = new ArrayList<SSys>();
	// 所属机构
	private List<SOrg> orgs = new ArrayList<SOrg>();
	// 所拥有角色
	private List<SRole> roles = new ArrayList<SRole>();
	// 所拥有岗位
	private List<SPost> posts = new ArrayList<SPost>();
	// 功能列表
	private List<SFunc> funcs = new ArrayList<SFunc>();
	// 功能点列表
	private List<SFuncAction> actions = new ArrayList<SFuncAction>();

	private transient JoinList orgList = null;

	@Unjson
	public JoinList getOrgList() {
		if (orgList == null) {
			QueryCache qc = new QueryCache("select distinct a.deptId from SUserOrg a where a.userId=:userId")
					.setParameter("userId", uuid);
			orgList = new JoinList(SOrg.class, qc);
		}
		return orgList;
	}

	private transient JoinList sysList = null;

	@Unjson
	public JoinList getSysList() {
		if (sysList == null) {
			QueryCache qc = new QueryCache(" select distinct a.uuid from SSys a where a.openFlag='1' and "
					+ " exists(select 1 from SUserRole b where a.uuid=b.sysId and b.userId = :userId) ")
							.setParameter("userId", uuid);
			sysList = new JoinList(SSys.class, qc);
		}
		return sysList;
	}

	private transient JoinList postList = null;

	@Unjson
	public JoinList getPostList() {
		if (postList == null) {
			QueryCache qc = new QueryCache("select distinct a.postId from SUserPost a where a.userId=:userId")
					.setParameter("userId", uuid);
			postList = new JoinList(SPost.class, qc);
		}
		return postList;
	}

	private transient JoinList roleList = null;

	@Unjson
	public JoinList getRoleList() {
		if (roleList == null) {
			StringBuffer sb = new StringBuffer(" select distinct a.roleId from SUserRole a where a.userId=:userId ");
			QueryCache qc = new QueryCache(sb.toString()).setParameter("userId", uuid);
			roleList = new JoinList(SRole.class, qc);
		}
		return roleList;
	}
	
	public void updateTotalLoginCount() {
		if (this != null) {
			TransactionCache tc = new TransactionCache();
			this.setLastLoginTime(Calendar.getInstance().getTime());
			this.setFailedLoginCount(0);
			this.setTotalLoginCount(this.getTotalLoginCount() + 1);
			tc.update(this);
			tc.commit();
		}
	}
	
	public void updateFailedLoginCount() {
		if (this != null) {
			TransactionCache tc = new TransactionCache();
			this.setFailedLoginCount(this.getFailedLoginCount() + 1);
			tc.update(this);
			tc.commit();
		}
	}
	
	public void updateSafeStatus() {
		if (this != null) {
			TransactionCache tc = new TransactionCache();
			this.setOpenFlag(Constants.NO);
			tc.update(this);
			tc.commit();
		}
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getSex() {
		return sex;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(String orderNum) {
		this.orderNum = orderNum;
	}

	public String getDelFlag() {
		return delFlag;
	}

	public void setDelFlag(String delFlag) {
		this.delFlag = delFlag;
	}

	public String getOpenFlag() {
		return openFlag;
	}

	public void setOpenFlag(String openFlag) {
		this.openFlag = openFlag;
	}

	public Date getIssueDate() {
		return issueDate;
	}

	public void setIssueDate(Date issueDate) {
		this.issueDate = issueDate;
	}

	public String getIssueId() {
		return issueId;
	}

	public void setIssueId(String issueId) {
		this.issueId = issueId;
	}

	public String getIssueName() {
		return issueName;
	}

	public void setIssueName(String issueName) {
		this.issueName = issueName;
	}

	public Date getEditDate() {
		return editDate;
	}

	public String getReviewPwd() {
		return reviewPwd;
	}

	public void setReviewPwd(String reviewPwd) {
		this.reviewPwd = reviewPwd;
	}

	public void setEditDate(Date editDate) {
		this.editDate = editDate;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public Integer getTotalLoginCount() {
		return totalLoginCount;
	}

	public void setTotalLoginCount(Integer totalLoginCount) {
		this.totalLoginCount = totalLoginCount;
	}

	public Integer getFailedLoginCount() {
		return failedLoginCount;
	}

	public void setFailedLoginCount(Integer failedLoginCount) {
		this.failedLoginCount = failedLoginCount;
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public String getUserId() {
		return this.uuid;
	}

	public void setUserId(String uid) {
		this.uuid = uid;

	}

	public String getType() {
		return this.userType;
	}

	public String getOrganId() {
		return this.orgId;
	}

	public String getStatus() {
		return this.openFlag;
	}

	public Date getEditPwdTime() {
		return editPwdTime;
	}

	public void setEditPwdTime(Date editPwdTime) {
		this.editPwdTime = editPwdTime;
	}

	public String getActiveStatus() {
		return activeStatus;
	}

	public void setActiveStatus(String activeStatus) {
		this.activeStatus = activeStatus;
	}

	public Date getActiveDeadLine() {
		return activeDeadLine;
	}

	public void setActiveDeadLine(Date activeDeadLine) {
		this.activeDeadLine = activeDeadLine;
	}

	public String getOrgName() {
		SOrg org = null;
		if (StringHelper.isEmpty(orgId))
			org = new SOrg();
		else {
			org = QueryCache.get(SOrg.class, orgId);
			if (org != null) {
				orgName = org.getName();
			}
		}
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public String getSecLevel() {
		return secLevel;
	}

	public void setSecLevel(String secLevel) {
		this.secLevel = secLevel;
	}

	public String getSkinId() {
		return skinId;
	}

	public void setSkinId(String skinId) {
		this.skinId = skinId;
	}

	@Override
	public java.util.Set<String> getFunctions() {
		return functions;
	}

	public void setFunctions(java.util.Set<String> functions) {
		if (functions != null) {
			this.functions.clear();
			this.functions.addAll(functions);
		}
	}

	@Override
	public java.util.Set<String> getFuncActions() {
		return funcActions;
	}

	public void setFuncActions(java.util.Set<String> funcActions) {
		if (funcActions != null) {
			this.funcActions.clear();
			this.funcActions.addAll(funcActions);
		}
	}

	@Override
	public String getPassword() {
		return password;
	}

	@SuppressWarnings("unchecked")
	public List<SSys> getSyss() {
		this.syss = sysList.getList();
		return this.syss;
	}

	@SuppressWarnings("unchecked")
	public java.util.List<SOrg> getOrgs() {
		this.orgs = orgList.getList();
		return this.orgs;
	}

	@SuppressWarnings("unchecked")
	public java.util.List<SRole> getRoles() {
		this.roles = roleList.getList();
		return this.roles;
	}

	@SuppressWarnings("unchecked")
	public java.util.List<SPost> getPosts() {
		this.posts = postList.getList();
		return this.posts;
	}

	public java.util.List<SFunc> getFuncs() {
		return funcs;
	}

	public java.util.List<SFuncAction> getActions() {
		return actions;
	}

	public void setSyss(List<SSys> syss) {
		this.syss = syss;
	}

	public void setOrgs(java.util.List<SOrg> orgs) {
		this.orgs = orgs;
	}

	public void setRoles(java.util.List<SRole> roles) {
		this.roles = roles;
	}

	public void setPosts(java.util.List<SPost> posts) {
		this.posts = posts;
	}

	public void setFuncs(java.util.List<SFunc> funcs) {
		this.funcs = funcs;
	}

	public void setActions(java.util.List<SFuncAction> actions) {
		this.actions = actions;
	}

	@SuppressWarnings("unchecked")
	public static String getUserPostName(String userId) {
		String postName = "";
		SUser item = QueryCache.get(SUser.class, userId);
		List<String> userPostList = item.getPostList().getListById();// 用户目前分配的岗位列表
		List<String> postIdList = new ArrayList<String>();
		for (String postid : userPostList) {
			postIdList.add(postid.trim());
			SPost post = QueryCache.get(SPost.class, postid.trim());
			postName += post.getName();
			postName += " ; ";
		}
		if (postName.length() > 2) {
			String post = postName.substring(0, postName.length() - 2);
			return post;
		} else {
			return postName;
		}
	}

	public String getStaffId() {
		return staffId;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public String getRoomNum() {
		return roomNum;
	}

	public void setRoomNum(String roomNum) {
		this.roomNum = roomNum;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getIdcard() {
		return idcard;
	}

	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}

	public String getContactor() {
		return contactor;
	}

	public void setContactor(String contactor) {
		this.contactor = contactor;
	}

	public String getContactphone() {
		return contactphone;
	}

	public void setContactphone(String contactphone) {
		this.contactphone = contactphone;
	}

	public String getNativeplace() {
		return nativeplace;
	}

	public void setNativeplace(String nativeplace) {
		this.nativeplace = nativeplace;
	}

	public String getBirthplace() {
		return birthplace;
	}

	public void setBirthplace(String birthplace) {
		this.birthplace = birthplace;
	}

	public String getLiveplace() {
		return liveplace;
	}

	public void setLiveplace(String liveplace) {
		this.liveplace = liveplace;
	}

	public String getNation() {
		return nation;
	}

	public void setNation(String nation) {
		this.nation = nation;
	}

	public String getHealth() {
		return health;
	}

	public void setHealth(String health) {
		this.health = health;
	}

	public Date getJoinworktime() {
		return joinworktime;
	}

	public void setJoinworktime(Date joinworktime) {
		this.joinworktime = joinworktime;
	}

	public Date getJoinpartytime() {
		return joinpartytime;
	}

	public void setJoinpartytime(Date joinpartytime) {
		this.joinpartytime = joinpartytime;
	}

	public String getSecondparty() {
		return secondparty;
	}

	public void setSecondparty(String secondparty) {
		this.secondparty = secondparty;
	}

	public String getThirdparty() {
		return thirdparty;
	}

	public void setThirdparty(String thirdparty) {
		this.thirdparty = thirdparty;
	}

	public String getManage_state() {
		return manage_state;
	}

	public void setManage_state(String manage_state) {
		this.manage_state = manage_state;
	}

	public String getPerson_type() {
		return person_type;
	}

	public void setPerson_type(String person_type) {
		this.person_type = person_type;
	}

	public String getManager_type() {
		return manager_type;
	}

	public void setManager_type(String manager_type) {
		this.manager_type = manager_type;
	}

	public String getSkill() {
		return skill;
	}

	public void setSkill(String skill) {
		this.skill = skill;
	}

	public void setOrgList(JoinList orgList) {
		this.orgList = orgList;
	}

	public void setSysList(JoinList sysList) {
		this.sysList = sysList;
	}

	public void setPostList(JoinList postList) {
		this.postList = postList;
	}

	public void setRoleList(JoinList roleList) {
		this.roleList = roleList;
	}

	public String getPolitical_landscape() {
		return political_landscape;
	}

	public void setPolitical_landscape(String political_landscape) {
		this.political_landscape = political_landscape;
	}

	public String getJoinpartycode() {
		return joinpartycode;
	}

	public void setJoinpartycode(String joinpartycode) {
		this.joinpartycode = joinpartycode;
	}

	public Date getLastmodifytime() {
		return lastmodifytime;
	}

	public void setLastmodifytime(Date lastmodifytime) {
		this.lastmodifytime = lastmodifytime;
	}

	public String getResume() {
		return resume;
	}

	public void setResume(String resume) {
		this.resume = resume;
	}

	public String getCurrent_duty() {
		return current_duty;
	}

	public void setCurrent_duty(String current_duty) {
		this.current_duty = current_duty;
	}

	public String getReward_and_punish() {
		return reward_and_punish;
	}

	public void setReward_and_punish(String reward_and_punish) {
		this.reward_and_punish = reward_and_punish;
	}

	public String getExamine_conclusion() {
		return examine_conclusion;
	}

	public void setExamine_conclusion(String examine_conclusion) {
		this.examine_conclusion = examine_conclusion;
	}

	public String getProfessiontech_duty() {
		return professiontech_duty;
	}

	public void setProfessiontech_duty(String professiontech_duty) {
		this.professiontech_duty = professiontech_duty;
	}

	public String getFull_time_education() {
		return full_time_education;
	}

	public void setFull_time_education(String full_time_education) {
		this.full_time_education = full_time_education;
	}

	public String getFull_time_school_major() {
		return full_time_school_major;
	}

	public void setFull_time_school_major(String full_time_school_major) {
		this.full_time_school_major = full_time_school_major;
	}

	public String getOn_the_job_education() {
		return on_the_job_education;
	}

	public void setOn_the_job_education(String on_the_job_education) {
		this.on_the_job_education = on_the_job_education;
	}

	public String getOn_the_job_school_major() {
		return on_the_job_school_major;
	}

	public void setOn_the_job_school_major(String on_the_job_school_major) {
		this.on_the_job_school_major = on_the_job_school_major;
	}
}